Imports GeoFramework.IO

Public Class SynchronizingTheClockForm

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Start receiving real-time GPS Data
        GpsDevice.Start()

    End Sub

    Private Sub ComputerClock_ValueChanged(ByVal sender As System.Object, ByVal e As GeoFramework.DateTimeEventArgs) Handles ComputerClock.ValueChanged

        ' Update the computer time on the form
        ComputerTimeLabel.Text = e.DateTime.ToLongTimeString()

    End Sub

    Private Sub GpsClock_ValueChanged(ByVal sender As System.Object, ByVal e As GeoFramework.DateTimeEventArgs) Handles GpsClock.ValueChanged

        ' Update the GPS time on the form
        GpsTimeLabel.Text = e.DateTime.ToLongTimeString()

        ' Also update the computer time to show how similar the clocks are
        ComputerTimeLabel.Text = DateTime.Now.ToLongTimeString()

        ' The GPS-derived date and time can actually be inaccurate until
        ' a satellite fix is acquired.  As a result, wait until a fix is present
        ' before comparing the computer clock with the GPS clock.
        If GpsDevice.IsFixed Then

            ' Calculate the difference between local time and satellite-derived time
            Dim TimeDifference As Double = e.DateTime.Subtract(DateTime.Now).TotalMilliseconds

            ' Is the local clock slow, fast, or precisely accurate?                
            If TimeDifference > 0 Then
                TimeDifferenceLabel.Text = "Computer clock is " + TimeDifference.ToString() + " milliseconds slow."
            ElseIf (TimeDifference < 0) Then
                TimeDifferenceLabel.Text = "Computer clock is " + Math.Abs(TimeDifference).ToString() + " milliseconds fast."
            Else
                TimeDifferenceLabel.Text = "Computer clock exactly matches GPS clock."
            End If

        Else

            ' No fix is present yet
            TimeDifferenceLabel.Text = "Waiting for satellite fix..."

        End If

    End Sub

    Private Sub SynchronizeCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SynchronizeCheckBox.CheckedChanged
        ' Enable or disable automatic clock synchronization
        Devices.IsClockSynchronizationEnabled = SynchronizeCheckBox.Checked
    End Sub
End Class